//-----------------------------------------------------------------------
// <copyright file="a_DocDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_DocDAOBase
    {
        #region Common methods
        public virtual a_Doc Createa_DocFromReader(IDataReader reader)
        {
            a_Doc item = new a_Doc();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("DocID"))) item.DocID = (int)reader["DocID"];
                if (!reader.IsDBNull(reader.GetOrdinal("PSEmailMes"))) item.PSEmailMes = (string)reader["PSEmailMes"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Doc methods
            
        public virtual void Createa_Doc(a_Doc a_Doc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Docs_ins");
                
                database.AddInParameter(dbCommand, "@PSEmailMes", DbType.String, a_Doc.PSEmailMes);
                database.AddOutParameter(dbCommand, "@DocID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Doc.DocID = (int)database.GetParameterValue(dbCommand, "@DocID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Doc methods
        
        public virtual void Updatea_Doc(a_Doc a_Doc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Docs_upd");            
                
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, a_Doc.DocID);
                database.AddInParameter(dbCommand, "@PSEmailMes", DbType.String, a_Doc.PSEmailMes);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Doc methods
        public virtual void Deletea_Doc(int docID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Docs_del");
                
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, docID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Doc methods
        
        public virtual a_Doc Geta_Doc(int docID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Docs_sel");
                
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, docID);
                
                a_Doc a_Doc =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Doc = Createa_DocFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Doc;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_DocList methods
        public virtual a_DocCollection Geta_DocList(a_DocColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Docs_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_DocCollection a_DocCollection = new a_DocCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Doc a_Doc = Createa_DocFromReader(reader);
                        a_DocCollection.Add(a_Doc);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_DocCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_DocCollection Geta_DocList(a_DocColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_DocList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}